#!/bin/bash
rpm -q expect
if [ $? -eq 0 ]; then
        echo "已安装"
else
        yum -y install expect
        if [ $? -eq 0 ]; then
                echo "安装成功"
        else
                echo "安装失败"
        fi
fi
#本机是否有ssh密钥 没有则生成
if [ ! -f ~/.ssh/id_rsa ]; then
	echo "请按3次enter健"
	ssh-keygen -t rsa
fi
ssh_expect() {
	expect -c "set timeout -1;
	spawn ssh-copy-id  root@$1

	expect {
		"yes/no" { send -- yes\r;exp_continue;}
		"password:" { send -- $2\r;exp_continue;}
		eof	
	}";
}

[ -f hosts.txt ] && rm -rf hosts.txt

#定义 hosts.txt
cat > hosts.txt << EOF
      10.0.0.1
      10.0.0.2
      10.0.0.3 
      10.0.0.4
EOF

passwd=redhat

for ip in `cat hosts.txt|awk '{print $1}'`
do
	ssh_expect ${ip} ${passwd}
done
